package com.example.dowertefense;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class MainActivity extends Activity implements OnClickListener, OnTouchListener{

	float x;
	float y;
	RelativeLayout rl;
	PlayGame game;
	String towerType;
	LinearLayout l1;
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		game = (PlayGame) findViewById(R.id.the_canvas);
		
		rl = (RelativeLayout) findViewById(R.id.my_layout);
		//game.addLayout(rl);
		
		Button button1 = (Button)findViewById(R.id.button1);
		button1.setOnClickListener(this);
		
		Button button2 = (Button)findViewById(R.id.button2);
		button2.setOnClickListener(this);
		
		Button button3 = (Button)findViewById(R.id.button3);
		button3.setOnClickListener(this);
		
		l1 = (LinearLayout) findViewById(R.id.linLayout);
		
		ImageView iv = new ImageView(this);
		iv.setImageResource(R.drawable.ic_launcher);
		rl.setOnTouchListener(this);
		towerType="";
		// TOP LEFT AND RIGHT POINTS
		//iv.setY(0);
		//iv.setX(-204); LEFT MOST POINT
		//iv.setX(204); RIGHT MOST
		
		// BOTTOM LEFT AND RIGHT POINTS
		//iv.setY(620);
		//iv.setX(-204); LEFT
		//iv.setX(-204); RIGHT
		iv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		
		
		//ll.addView(iv);
		//rl.addView(iv);
		game.start();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v.getId() == R.id.button1)
		{
			towerType = "tower1";
			l1.setVisibility(View.GONE);
		}
		else if(v.getId() == R.id.button2)
		{
			towerType = "tower2";
			l1.setVisibility(View.GONE);
		}
		else if(v.getId() == R.id.button3)
		{
			v.setVisibility(View.GONE);
			l1.setVisibility(View.VISIBLE);
			game.Pause();
		}	
	}
	
	@SuppressLint("NewApi")
	@Override
	public boolean onTouch(View arg0, MotionEvent arg1) {
		// TODO Auto-generated method stub
		if(towerType == "tower1")
		{
			x = arg1.getRawX();
			y = arg1.getRawY();
			ImageView iv = new ImageView(this);
			iv.setImageResource(R.drawable.tower1);
			iv.setX(x - 238);
			iv.setY(y - 142);
			iv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			
			//ll.addView(iv);
			rl.addView(iv);
			towerType = "";
			l1.setVisibility(View.GONE);
			((Button)findViewById(R.id.button3)).setVisibility(View.VISIBLE);
			game.callMe(x, y);
		}
		else if(towerType == "tower2")
		{
			x = arg1.getRawX();
			y = arg1.getRawY();
			ImageView iv = new ImageView(this);
			iv.setImageResource(R.drawable.tower2);
			iv.setX(x - 238);
			iv.setY(y - 142);
			iv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			
			//ll.addView(iv);
			rl.addView(iv);
			towerType = "";
			l1.setVisibility(View.GONE);
			((Button)findViewById(R.id.button3)).setVisibility(View.VISIBLE);
			game.callMe(x, y);
		}
		game.Pause();
		return false;
	}

}
